define(['jquery','mage/translate','Magento_Customer/js/customer-data','jquery-ui-modules/widget','jquery/jquery-storageapi'],function($,$t,customerData){'use strict';return function(target){$.widget('mage.addToWishlist',target,{wishlist:false,processStop:false,isProductInfoMain:false,_create:function(){this._super();if(this.options.wishlist_added_active){this.isProductInfoMain=this.element.hasClass('product-info-main');this.wishlist=customerData.get('wishlist');this.customer=customerData.get('customer');if(this.wishlist().counter){this._setWishlistStatus();} this.wishlist.subscribe(function(){this._setWishlistStatus();if(this.processStop){$('body').trigger('processStop');}}.bind(this));this.bindAjaxFormSubmit();this.bindSuperAttributeChange();}},bindAjaxFormSubmit:function(){this.element.on('click','[data-action="add-to-wishlist"]',function(event){if(this.customer().firstname){var params,action;this.currentElem=$(event.currentTarget);params=$(event.currentTarget).data('post');action=params.action;let wishlistItemToRemove=$(event.currentTarget).data('wishlist-item');if(wishlistItemToRemove){let deleteParams=JSON.parse(wishlistItemToRemove.delete_item_params) action=deleteParams.action;$.extend(params.data,deleteParams.data)} event.stopPropagation();event.preventDefault();params.data.is_ajax=true;params.data.form_key=$.cookie('form_key');this.processStop=true;$.ajax({url:action,data:params.data,type:'POST',context:this,beforeSend:function(){$('body').trigger('processStart');$.cookieStorage.set('mage-messages','');},success:function(res){var itemInfo='',pdpClass='';var haveError=0;var errorMessage='';var mageMessages=$.cookieStorage.get('mage-messages');if(mageMessages instanceof Array&&mageMessages.length){if('type'in mageMessages[0]){if(mageMessages[0]['type']=='error'){haveError=1;errorMessage=mageMessages[0]['text'];}}} if($('body').hasClass('catalog-product-view')){itemInfo=this.currentElem.closest('.column.main');pdpClass='message-success success message';if(haveError){pdpClass='message-error error message';}}else{itemInfo=this.currentElem.closest('.product-item-info').find('.product-item-container');} if(!wishlistItemToRemove){customerData.reload(['wishlist']);var resultMessage=$t('Se ha agregado a su lista de favoritos.');if(haveError){resultMessage=errorMessage;} itemInfo.prepend('');}else{itemInfo.prepend('');} customerData.reload(['wishlist','messages']);var wishlistAddedMsg=itemInfo.find('.wishlist-msg-ajax');wishlistAddedMsg.fadeIn(400);setTimeout(function(){wishlistAddedMsg.fadeOut(300,function(){$(this).remove();});},6000);},error:function(res){customerData.reload(['messages']);$('body').trigger('processStop');this.processStop=false;}});}}.bind(this));this.element.on('click','.custom-swatches .swatch-option',function(event){var selectedProdId=$(event.currentTarget).data("product-id");var selectedProdWishlist=$(event.currentTarget).closest('.product-item-info').find('.action.towishlist');var selectedProdDataPost=selectedProdWishlist.data('post');if(selectedProdDataPost){selectedProdDataPost.data.product=selectedProdId;selectedProdWishlist.attr('data-post',JSON.stringify(selectedProdDataPost));this._setWishlistStatus();}}.bind(this));},bindFormSubmit:function(){var self=this;this.element.find('[data-action="add-to-wishlist"]').on('click',function(event){var element,params,form,action;event.stopPropagation();event.preventDefault();element=$('input[type=file]'+self.options.customOptionsInfo);params=$(event.currentTarget).data('post');form=$(element).closest('form');action=params.action;if(params.data.id){$('',{type:'hidden',name:'id',value:params.data.id}).appendTo(form);} if(params.data.uenc){action+='uenc/'+params.data.uenc;} $(form).attr('action',action).trigger('submit');});},bindSuperAttributeChange:function(){this.element.on('change','input[name*=super_attribute]',function(){this._setWishlistStatus();}.bind(this));},_setWishlistStatus:function(){this.element.find('[data-action="add-to-wishlist"]').removeClass('wishlist-added');this.element.find('[data-action="add-to-wishlist"]').removeData('wishlist-item');if(this.wishlist().counter){this.wishlist().items_added.forEach(function(item){let addToWishlistLink,productForm,isInWishlist,attribute;this.element.find('[data-action="add-to-wishlist"]').filter('[data-post*="\\"product\\":'+item.product_id+',"]').each(function(i,elem){isInWishlist=true;addToWishlistLink=$(elem);if(this.isProductInfoMain){productForm=this.element.find('#product_addtocart_form');}else{productForm=addToWishlistLink.closest('.product-item').find('[data-role=tocart-form]');} if(productForm.length){if(item.super_attribute){Object.keys(item.super_attribute).forEach(function(code){attribute=productForm.find('[name=super_attribute\\['+code+'\\]]');if(attribute.length&&attribute.val()!=item.super_attribute[code]){isInWishlist=false;}});}else{productForm.find('input.swatch-input').each(function(i,input){if(input.value){isInWishlist=false;}});}} if(isInWishlist){addToWishlistLink.addClass('wishlist-added');addToWishlistLink.data('wishlist-item',item);}}.bind(this));},this);}}});return $.mage.addToWishlist;};});